package com.linkvin.ec.hcs.service.security.factory;

import com.linkvin.ec.hcs.dao.model.sysadmin.bo.AccountUserBo;
import com.linkvin.ec.hcs.service.security.domains.JwtUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by qichao on 2017/11/29.
 */
public class JwtUserFactory {

    public JwtUserFactory() {
    }

    public static JwtUser create(AccountUserBo accountUserBo) {
        return new JwtUser(accountUserBo.getAccountId(), accountUserBo.getAccountCode(),
                accountUserBo.getUserCode(), accountUserBo.getUserName(),
                accountUserBo.getPwd(), accountUserBo.getMobile(),
                accountUserBo.getEmail(),accountUserBo.getEntityId(), accountUserBo.getUserId(), mapToGrantedAuthorities(accountUserBo.getRoles()));
    }

    public static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        if (!CollectionUtils.isEmpty(authorities)) {
            return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }
        return null;
    }
}
